package com.daykw.xiaohongshu.http;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.net.Authenticator;
import java.net.ConnectException;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.PasswordAuthentication;
import java.net.Proxy;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.Charset;
import java.nio.charset.UnsupportedCharsetException;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;
import java.util.Set;
import java.util.zip.GZIPInputStream;

import javax.net.ssl.HttpsURLConnection;

import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpClientParams;
import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.ParseException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.CharArrayBuffer;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;

import com.daykw.xiaohongshu.model.Ips;
import com.daykw.xiaohongshu.util.Const;
import com.daykw.xiaohongshu.util.IPPoolMap;

public class HttpBase {

	private static Logger logger = Logger.getLogger(HttpBase.class);
	private static final java.util.Random RANDOM = new java.util.Random();

	public synchronized String post(String url, Map<String, String> header, Map<String, String> params,boolean isproxy)
			throws ConnectException {
		Ips ips = null;
		PrintWriter out = null;
		BufferedReader in = null;
		String result = "";
		try {
			URL realUrl = new URL(url);
			URLConnection conn=null;
			// 打开和URL之间的连接
			if (isproxy) {
				ips = IPPoolMap.getQueue().take();

				Calendar beforeTime = Calendar.getInstance();
				beforeTime.add(Calendar.MINUTE, -3);// 5分钟之前的时间
				Date be = beforeTime.getTime();
				while (be.after(ips.getCreate_time())) {
					ips = IPPoolMap.getQueue().take();
					IPPoolMap.removeIp(ips.getIps());
				}

				String host = ips.getIps().split(":")[0];
				Integer port = Integer.parseInt(ips.getIps().split(":")[1]);
				Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(host, port));
				conn = realUrl.openConnection(proxy);
			} else {
				conn = realUrl.openConnection();
			}
			// 设置通用的请求属性
			conn.setRequestProperty("accept", "application/json, text/javascript, */*; q=0.01");
			conn.setRequestProperty("connection", "Keep-Alive");
			conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
			Set<String> headers = header.keySet();
			for (String key : headers) {
				conn.setRequestProperty(key, header.get(key));
			}
			/**
			 * 参数
			 */
			StringBuffer p = new StringBuffer();
			Set<String> param = params.keySet();
			for (String k : param) {
				p.append(k + "=" + params.get(k) + "&");
			}

			if (p.length() > 0) {
				p = p.deleteCharAt(p.length() - 1);
			}

			// 发送POST请求必须设置如下两行
			conn.setConnectTimeout(30000);
			conn.setReadTimeout(30000);
			conn.setDoOutput(true);
			conn.setDoInput(true);
			// 获取URLConnection对象对应的输出流
			out = new PrintWriter(conn.getOutputStream());
			// 发送请求参数
			out.print(p.toString());
			// flush输出流的缓冲
			out.flush();
			// 定义BufferedReader输入流来读取URL的响应
			in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
			String line;
			while ((line = in.readLine()) != null) {
				result += line;
			}
		} catch (Exception e) {
			System.out.println("发送 POST 请求出现异常！" + e);
			throw new java.net.ConnectException("请求错误,status code is[" + result + "]");
		}
		// 使用finally块来关闭输出流、输入流
		finally {
			try {
				if (out != null) {
					out.close();
				}
				if (in != null) {
					in.close();
				}
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
		return result;
	}

	/**
	 * Post请求
	 * 
	 * @return
	 * @throws Exception
	 */
	public String post(String url, String charset, Map<String, String> header, Map<String, String> data)
			throws Exception {
		CustomPostMethod postMethod = new CustomPostMethod(url);
		try {
			HttpClient client = getClient(charset);
			postMethod.setRequestHeader("User-Agent",
					"Mozilla/5.0 (Windows NT " + (RANDOM.nextInt(5) + 1) + ".1) AppleWebKit/"
							+ (RANDOM.nextInt(537) + 1) + "." + (RANDOM.nextInt(11) + 1)
							+ " (KHTML, like Gecko) Chrome/" + (RANDOM.nextInt(23) + 1) + ".0."
							+ (RANDOM.nextInt(1271) + 1) + "." + (RANDOM.nextInt(95) + 1) + " Safari/"
							+ (RANDOM.nextInt(537) + 1) + "." + (RANDOM.nextInt(11) + 1) + "");
			postMethod.setRequestHeader("Accept-Encoding", "gzip,deflate,sdch");
			Set<String> headers = header.keySet();
			for (String key : headers) {
				postMethod.setRequestHeader(key, header.get(key));
			}
			Set<String> keys = data.keySet();
			NameValuePair[] ps = new NameValuePair[keys.size()];
			int index = 0;
			for (String k : keys) {
				ps[index++] = new NameValuePair(k, data.get(k));
			}
			postMethod.setRequestBody(ps);

			int statusCode = client.executeMethod(postMethod);// 回车——出拳！
			if (statusCode == 200) {
				String result = postMethod.getResponseBodyAsString();

				return result;
			}
			postMethod.abort();
			throw new java.net.ConnectException("请求错误,status code is[" + statusCode + "]");
		} catch (Exception e) {
			postMethod.abort();
			throw e;
		} finally {
			postMethod.releaseConnection();
		}

	}

	public String turl(String url, String charset, Map<String, String> header, boolean isproxy) throws Exception {
		Ips ips = null;
		try {
			URL u = new URL(url);
			// if ("https".equalsIgnoreCase(u.getProtocol())) {
			SslUtils.ignoreSsl();
			// }
			URLConnection httpsConn;
			if (isproxy) {
				ips = IPPoolMap.getQueue().take();

				Calendar beforeTime = Calendar.getInstance();
				beforeTime.add(Calendar.MINUTE, -3);// 5分钟之前的时间
				Date be = beforeTime.getTime();
				while (be.after(ips.getCreate_time())) {
					ips = IPPoolMap.getQueue().take();
					IPPoolMap.removeIp(ips.getIps());
				}

				String host = ips.getIps().split(":")[0];
				Integer port = Integer.parseInt(ips.getIps().split(":")[1]);
				Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(host, port));
				httpsConn = u.openConnection(proxy);
			} else {
				httpsConn = u.openConnection();
			}
			// if (httpsConn instanceof HttpURLConnection ) {
			// HttpURLConnection httpc = (HttpURLConnection ) httpsConn;
			// httpc.setChunkedStreamingMode(1024);
			// }else if(httpsConn instanceof HttpsURLConnection ){
			// HttpsURLConnection httpc = (HttpsURLConnection ) httpsConn;
			// httpc.setChunkedStreamingMode(1024);
			// }

			if (header != null && !header.isEmpty()) {
				Set<String> keys = header.keySet();
				for (String key : keys) {
					httpsConn.setRequestProperty(key, header.get(key));
				}
			} else {
				httpsConn.setRequestProperty("User-Agent",
						"Mozilla/5.0 (Windows NT " + (RANDOM.nextInt(5) + 1) + ".1) AppleWebKit/"
								+ (RANDOM.nextInt(537) + 1) + "." + (RANDOM.nextInt(11) + 1)
								+ " (KHTML, like Gecko) Chrome/" + (RANDOM.nextInt(23) + 1) + ".0."
								+ (RANDOM.nextInt(1271) + 1) + "." + (RANDOM.nextInt(95) + 1) + " Safari/"
								+ (RANDOM.nextInt(537) + 1) + "." + (RANDOM.nextInt(11) + 1) + "");
				httpsConn.setRequestProperty("Accept-Encoding", "gzip,deflate,sdch");
			}
			httpsConn.setConnectTimeout(5000);
			httpsConn.setReadTimeout(5000);
			// 取得该连接的输入流，以读取响应内容
			BufferedReader insr = new BufferedReader(
					new InputStreamReader(new GZIPInputStream(httpsConn.getInputStream())));

			// 读取服务器的响应内容并显示
			StringBuffer sb = new StringBuffer();
			String s;
			while ((s = insr.readLine()) != null) {
				sb.append(s + "\n");
			}
			if (httpsConn instanceof HttpURLConnection) {
				HttpURLConnection conn = (HttpURLConnection) httpsConn;
				if (conn.getResponseCode() != 200) {
					if (ips != null) {
						IPPoolMap.removeIp(ips.getIps());
						return "fail";
					}
				}
			} else if (httpsConn instanceof HttpsURLConnection) {
				HttpsURLConnection conn = (HttpsURLConnection) httpsConn;
				if (conn.getResponseCode() != 200) {
					if (ips != null) {
						IPPoolMap.removeIp(ips.getIps());
						return "fail";
					}
				}
			}

			if (sb.toString().contains(
					"Authentication failed either because credentials were not provided or they could not be validated")
					|| sb.toString().contains("You are being redirected to the authentication virtual host.")
					|| sb.toString().contains("You are being redirected to the authentication virtual host.")) {
				if (ips != null) {
					IPPoolMap.removeIp(ips.getIps());
					return "fail";
				}
			}

			return sb.toString();
		} catch (java.net.ConnectException e) {
			if (ips != null) {
				IPPoolMap.removeIp(ips.getIps());
				return "fail";
			} else {
				throw e;
			}

		} catch (java.net.SocketTimeoutException e) {
			if (ips != null) {
				IPPoolMap.removeIp(ips.getIps());
				return "fail";
			} else {
				throw e;
			}
		} catch (java.net.SocketException e) {
			if (ips != null) {
				IPPoolMap.removeIp(ips.getIps());
				return "fail";
			} else {
				throw e;
			}
		} catch (org.apache.commons.httpclient.ConnectTimeoutException e) {
			if (ips != null) {
				IPPoolMap.removeIp(ips.getIps());
				return "fail";
			} else {
				throw e;
			}
		} catch (javax.net.ssl.SSLHandshakeException e) {
			if (ips != null) {
				IPPoolMap.removeIp(ips.getIps());
				return "fail";
			} else {
				throw e;
			}
		} catch (Exception e) {
			if (ips != null) {
				IPPoolMap.removeIp(ips.getIps());
				return "fail";
			} else {
				throw e;
			}
		}
	}

	public String aurl(String url, String charset, Map<String, String> header, boolean isproxy) throws Exception {
		Ips ips = null;
		try {
			URL u = new URL(url);
			// if ("https".equalsIgnoreCase(u.getProtocol())) {
			SslUtils.ignoreSsl();
			// }
			URLConnection httpsConn;
			if (isproxy) {
				ips = IPPoolMap.getQueue().take();

				Calendar beforeTime = Calendar.getInstance();
				beforeTime.add(Calendar.MINUTE, -6);// 5分钟之前的时间
				Date be = beforeTime.getTime();
				while (be.after(ips.getCreate_time())) {
					ips = IPPoolMap.getQueue().take();
					IPPoolMap.removeIp(ips.getIps());
				}

				String host = ips.getIps().split(":")[0];
				Integer port = Integer.parseInt(ips.getIps().split(":")[1]);
				Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(host, port));
				httpsConn = u.openConnection(proxy);
			} else {
				httpsConn = u.openConnection();
			}

			if (header != null && !header.isEmpty()) {
				Set<String> keys = header.keySet();
				for (String key : keys) {
					httpsConn.setRequestProperty(key, header.get(key));
				}
			} else {
				httpsConn.setRequestProperty("User-Agent",
						"Mozilla/5.0 (Windows NT " + (RANDOM.nextInt(5) + 1) + ".1) AppleWebKit/"
								+ (RANDOM.nextInt(537) + 1) + "." + (RANDOM.nextInt(11) + 1)
								+ " (KHTML, like Gecko) Chrome/" + (RANDOM.nextInt(23) + 1) + ".0."
								+ (RANDOM.nextInt(1271) + 1) + "." + (RANDOM.nextInt(95) + 1) + " Safari/"
								+ (RANDOM.nextInt(537) + 1) + "." + (RANDOM.nextInt(11) + 1) + "");
				httpsConn.setRequestProperty("Accept-Encoding", "gzip,deflate,sdch");
			}
			httpsConn.setConnectTimeout(5000);
			httpsConn.setReadTimeout(5000);
			// 取得该连接的输入流，以读取响应内容
			BufferedReader insr = new BufferedReader(
					new InputStreamReader(new GZIPInputStream(httpsConn.getInputStream())));

			// 读取服务器的响应内容并显示
			StringBuffer sb = new StringBuffer();
			String s;
			while ((s = insr.readLine()) != null) {
				sb.append(s + "\n");
			}
			if (httpsConn instanceof HttpURLConnection) {
				HttpURLConnection conn = (HttpURLConnection) httpsConn;
				if (conn.getResponseCode() != 200) {
					if (ips != null) {
						IPPoolMap.removeIp(ips.getIps());
						return "fail";
					}
				}
			} else if (httpsConn instanceof HttpsURLConnection) {
				HttpsURLConnection conn = (HttpsURLConnection) httpsConn;
				if (conn.getResponseCode() != 200) {
					if (ips != null) {
						IPPoolMap.removeIp(ips.getIps());
						return "fail";
					}
				}
			}

			if (sb.toString().contains(
					"Authentication failed either because credentials were not provided or they could not be validated")
					|| sb.toString().contains("You are being redirected to the authentication virtual host.")
					|| sb.toString().contains("You are being redirected to the authentication virtual host.")) {
				if (ips != null) {
					IPPoolMap.removeIp(ips.getIps());
					return "fail";
				}
			}

			return sb.toString();
		} catch (java.net.ConnectException e) {
			if (ips != null) {
				IPPoolMap.removeIp(ips.getIps());
				return "fail";
			} else {
				throw e;
			}

		} catch (java.net.SocketTimeoutException e) {
			if (ips != null) {
				IPPoolMap.removeIp(ips.getIps());
				return "fail";
			} else {
				throw e;
			}
		} catch (java.net.SocketException e) {
			if (ips != null) {
				IPPoolMap.removeIp(ips.getIps());
				return "fail";
			} else {
				throw e;
			}
		} catch (org.apache.commons.httpclient.ConnectTimeoutException e) {
			if (ips != null) {
				IPPoolMap.removeIp(ips.getIps());
				return "fail";
			} else {
				throw e;
			}
		} catch (javax.net.ssl.SSLHandshakeException e) {
			if (ips != null) {
				IPPoolMap.removeIp(ips.getIps());
				return "fail";
			} else {
				throw e;
			}
		} catch (Exception e) {
			if (ips != null) {
				IPPoolMap.removeIp(ips.getIps());
				return "fail";
			} else {
				throw e;
			}
		}

	}

	public  String agets(String url, String charset, Map<String, String> header, boolean isproxy)
			throws Exception {
		CloseableHttpClient httpclient = HttpClients.createDefault();
		Ips ips = null;
		try {
			RequestConfig config = null;
			if (isproxy) {

				ips = IPPoolMap.getQueue().take();

				Calendar beforeTime = Calendar.getInstance();
				beforeTime.add(Calendar.MINUTE, -4);// 5分钟之前的时间

				Date be = beforeTime.getTime();
				while (be.after(ips.getCreate_time())) {
					ips = IPPoolMap.getQueue().take();
				}
				String host = ips.getIps().split(":")[0];
				Integer port = Integer.parseInt(ips.getIps().split(":")[1]);

				HttpHost proxy = new HttpHost(host, port, "http");
				config = RequestConfig.custom().setProxy(proxy).build();

			}

			HttpGet httpGet = new HttpGet(url);
			httpGet.setConfig(config);

			if (header != null) {
				Set<String> keys = header.keySet();
				for (String key : keys) {
					httpGet.setHeader(key, header.get(key));
				}
			}
			// 设置参数到请求对象中

			// System.out.println("Executing request " +
			// request.getRequestLine() + " to " + target + " via " + proxy);
			CloseableHttpResponse response = httpclient.execute(httpGet);
			try {
				int statusCode = response.getStatusLine().getStatusCode();
				// System.out.println("----------------------------------------");
				if (statusCode == 200) {
					String result = EntityUtils.toString(response.getEntity());

					if (result.contains(
							"Authentication failed either because credentials were not provided or they could not be validated")
							|| result.toString()
									.contains("You are being redirected to the authentication virtual host.")
							|| result.toString()
									.contains("You are being redirected to the authentication virtual host.")
							|| result.toString().contains("400 Bad Request")
							|| result.toString().contains("404 Bad Request")) {
						if (ips != null) {
							IPPoolMap.removeIp(ips.getIps());
							return "fail";
						}
					}

					return result;
				} else {

					if (statusCode == 404) {
						return "404";
					} else {
						IPPoolMap.removeIp(ips.getIps());
						return "fail";
					}

				}

			} catch (java.net.ConnectException e) {
				if (ips != null) {
					IPPoolMap.removeIp(ips.getIps());
					return "fail";
				} else {
					throw e;
				}

			} catch (java.net.SocketTimeoutException e) {
				if (ips != null) {
					IPPoolMap.removeIp(ips.getIps());
					return "fail";
				} else {
					throw e;
				}
			} catch (java.net.SocketException e) {
				if (ips != null) {
					IPPoolMap.removeIp(ips.getIps());
					return "fail";
				} else {
					throw e;
				}
			} catch (org.apache.commons.httpclient.ConnectTimeoutException e) {
				if (ips != null) {
					IPPoolMap.removeIp(ips.getIps());
					return "fail";
				} else {
					throw e;
				}
			} catch (javax.net.ssl.SSLHandshakeException e) {
				if (ips != null) {
					IPPoolMap.removeIp(ips.getIps());
					return "fail";
				} else {
					throw e;
				}
			} catch (Exception e) {
				IPPoolMap.removeIp(ips.getIps());
				return "fail";

			} finally {

				response.close();
			}
		} catch (Exception e) {
			IPPoolMap.removeIp(ips.getIps());
			return "fail";
		} finally {
			httpclient.close();
		}
	}

	public static String authHeader() {

		int timestamp = (int) (new Date().getTime() / 1000);
		// 拼装签名字符串
		String planText = String.format("orderno=%s,secret=%s,timestamp=%d", Const.XD_ORDER, Const.XD_SCERT, timestamp);

		// 计算签名
		String sign = org.apache.commons.codec.digest.DigestUtils.md5Hex(planText).toUpperCase();

		// 拼装请求头Proxy-Authorization的值
		String authHeader = String.format("sign=%s&orderno=%s&timestamp=%d", sign, Const.XD_ORDER, timestamp);
		return authHeader;
	}

	public String get(String url, String charset, Map<String, String> header, boolean isproxy) throws Exception {
		CustomGetMethod getMethod = new CustomGetMethod(url);
		Ips ips = null;

		try {
			HttpClient client = getClient(charset);

			if (isproxy) {

				ips = IPPoolMap.getQueue().take();

				Calendar beforeTime = Calendar.getInstance();
				beforeTime.add(Calendar.MINUTE, -4);// 5分钟之前的时间

				Date be = beforeTime.getTime();
				while (ips != null && be.after(ips.getCreate_time())) {
					ips = IPPoolMap.getQueue().take();
					IPPoolMap.removeIp(ips.getIps());
				}

				String host = ips.getIps().split(":")[0];
				Integer port = Integer.parseInt(ips.getIps().split(":")[1]);

				client.getHostConfiguration().setProxy(host, port);

			}
	

			

			if (header != null && !header.isEmpty()) {
				Set<String> keys = header.keySet();
				for (String key : keys) {
					getMethod.setRequestHeader(key, header.get(key));
				}
			}

			int statusCode = client.executeMethod(getMethod);// 回车——出拳！
			if (statusCode == 200) {
				// getResponseBodyAsString
				String result = getMethod.getResponseBodyAsString();
				if (result.toString().contains(
						"Authentication failed either because credentials were not provided or they could not be validated")
						|| result.toString().contains("You are being redirected to the authentication virtual host.")
						|| result.toString().contains("You are being redirected to the authentication virtual host.")
						|| result.toString().contains("400 Bad Request")
						|| result.toString().contains("404 Bad Request")) {
					if (ips != null) {
						IPPoolMap.removeIp(ips.getIps());
						return "fail";
					}
				}
				return result;
			} else {
				if (statusCode == 404) {
					return "404";
				} else {
					IPPoolMap.removeIp(ips.getIps());
					return "fail";
				}

			}

		} catch (java.net.ConnectException e) {
			if (ips != null) {
				IPPoolMap.removeIp(ips.getIps());
				return "fail";
			} else {
				throw e;
			}

		} catch (java.net.SocketTimeoutException e) {
			if (ips != null) {
				IPPoolMap.removeIp(ips.getIps());
				return "fail";
			} else {
				throw e;
			}
		} catch (java.net.SocketException e) {
			if (ips != null) {
				IPPoolMap.removeIp(ips.getIps());
				return "fail";
			} else {
				throw e;
			}
		} catch (org.apache.commons.httpclient.ConnectTimeoutException e) {
			if (ips != null) {
				IPPoolMap.removeIp(ips.getIps());
				return "fail";
			} else {
				throw e;
			}
		} catch (javax.net.ssl.SSLHandshakeException e) {
			if (ips != null) {
				IPPoolMap.removeIp(ips.getIps());
				return "fail";
			} else {
				throw e;
			}
		} catch (Exception e) {
			IPPoolMap.removeIp(ips.getIps());
			return "fail";

		} finally {
			getMethod.abort();

			getMethod.releaseConnection();
		}

	}

	public String fget(String url, String charset, Map<String, String> header, boolean isproxy) throws Exception {
		CustomGetMethod getMethod = new CustomGetMethod(url);

		try {
			HttpClient client = getClient(charset);

			if (isproxy) {

				String host = "forward.xdaili.cn";
				Integer port = 80;

				client.getHostConfiguration().setProxy(host, port);
				getMethod.setRequestHeader("Proxy-Authorization", authHeader());
			}
			/** header */
			// "User-Agent","Mozilla/5.0 /Windows; U; Windows NT 4.1; de;
			// rv:1.9.1.5) Gecko/20091102 Firefox/3.0"
			// getMethod.setRequestHeader(header);
			getMethod.setRequestHeader("User-Agent",
					"Mozilla/5.0 (Windows NT " + (RANDOM.nextInt(5) + 1) + ".1) AppleWebKit/"
							+ (RANDOM.nextInt(537) + 1) + "." + (RANDOM.nextInt(11) + 1)
							+ " (KHTML, like Gecko) Chrome/" + (RANDOM.nextInt(23) + 1) + ".0."
							+ (RANDOM.nextInt(1271) + 1) + "." + (RANDOM.nextInt(95) + 1) + " Safari/"
							+ (RANDOM.nextInt(537) + 1) + "." + (RANDOM.nextInt(11) + 1) + "");
			getMethod.setRequestHeader("Accept-Encoding", "gzip,deflate,sdch");

			if (header != null && !header.isEmpty()) {
				Set<String> keys = header.keySet();
				for (String key : keys) {
					getMethod.setRequestHeader(key, header.get(key));
				}
			}

			int statusCode = client.executeMethod(getMethod);// 回车——出拳！
			if (statusCode == 200) {
				// getResponseBodyAsString
				String result = getMethod.getResponseBodyAsString();
				if (result.toString().contains(
						"Authentication failed either because credentials were not provided or they could not be validated")
						|| result.toString().contains("You are being redirected to the authentication virtual host.")
						|| result.toString().contains("You are being redirected to the authentication virtual host.")) {

				}
				return result;
			} else {

				// logger.error("请求错误,status code is[" + statusCode + "]");
				return "404";
			}

		} catch (java.net.ConnectException e) {

			return "fail";

		} catch (java.net.SocketTimeoutException e) {

			return "fail";

		} catch (java.net.SocketException e) {

			return "fail";

		} catch (org.apache.commons.httpclient.ConnectTimeoutException e) {

			return "fail";

		} catch (javax.net.ssl.SSLHandshakeException e) {

			return "fail";

		} catch (Exception e) {

			return "fail";

		} finally {
			getMethod.abort();

			getMethod.releaseConnection();
		}

	}

	private HttpClient getClient(String charset) {
		MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
		Integer MAX_TIME_OUT = 5000;
		// Integer MAX_IDLE_TIME_OUT = 60000;
		Integer MAX_CONN = 5000;
		connectionManager.closeIdleConnections(MAX_TIME_OUT);
		connectionManager.getParams().setParameter("http.connection-manager.max-total", MAX_CONN);
		HttpClient client = new HttpClient(connectionManager);
		HttpConnectionManagerParams params = client.getHttpConnectionManager().getParams();
		params.setConnectionTimeout(5000);
		params.setSoTimeout(5000);
		params.setDefaultMaxConnectionsPerHost(500);
		params.setMaxTotalConnections(1500);
		params.setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());

		HttpClientParams hcp = client.getParams();
		// hcp.setParameter("http.connection.timeout", 5 * 1000);
		// hcp.setParameter("http.connection-manager.timeout", 5 * 1000);
		hcp.setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, charset);
		hcp.setAuthenticationPreemptive(true);
		return client;
	}

	public String getResponseBodyAsString(GetMethod getMethod) throws IOException {

		GZIPInputStream gzin;

		if (getMethod.getResponseBody() != null || getMethod.getResponseBodyAsStream() != null) {

			if (getMethod.getResponseHeader("Content-Encoding") != null

					&& getMethod.getResponseHeader("Content-Encoding").getValue().toLowerCase().indexOf("gzip") > -1) {

				// For GZip response

				InputStream is = getMethod.getResponseBodyAsStream();

				gzin = new GZIPInputStream(is);

				InputStreamReader isr = new InputStreamReader(gzin, getMethod.getResponseCharSet());

				java.io.BufferedReader br = new java.io.BufferedReader(isr);

				StringBuffer sb = new StringBuffer();

				String tempbf;

				while ((tempbf = br.readLine()) != null) {
					sb.append(tempbf);
					sb.append("\r\n");
				}
				isr.close();
				gzin.close();
				return sb.toString();
			} else {
				// For deflate response
				return getMethod.getResponseBodyAsString();
			}
		} else {
			return null;
		}

	}

	public String enCodetoString(final HttpEntity entity, final String defaultCharset)
			throws IOException, ParseException {
		return enCodetoStringDo(entity, defaultCharset != null ? Charset.forName(defaultCharset) : null);
	}

	public String enCodetoStringDo(final HttpEntity entity, Charset defaultCharset) throws IOException, ParseException {
		if (entity == null) {
			throw new IllegalArgumentException("HTTP entity may not be null");
		}
		InputStream instream = entity.getContent();
		if (instream == null) {
			return null;
		}
		try {
			if (entity.getContentLength() > Integer.MAX_VALUE) {
				throw new IllegalArgumentException("HTTP entity too large to be buffered in memory");
			}
			int i = (int) entity.getContentLength();
			if (i < 0) {
				i = 4096;
			}
			Charset charset = null;
			try {
				// ContentType contentType = ContentType.get(entity);
				// if (contentType != null) {
				// charset = contentType.getCharset();
				// }
			} catch (final UnsupportedCharsetException ex) {
				throw new UnsupportedEncodingException(ex.getMessage());
			}
			if (charset == null) {
				charset = defaultCharset;
			}
			if (charset == null) {
				charset = HTTP.DEF_CONTENT_CHARSET;
			}
			Reader reader = new InputStreamReader(instream, charset);
			CharArrayBuffer buffer = new CharArrayBuffer(i);
			char[] tmp = new char[1024];
			int l;
			while ((l = reader.read(tmp)) != -1) {
				buffer.append(tmp, 0, l);
			}
			return buffer.toString();
		} finally {
			instream.close();
		}
	}

	public synchronized String http(String url, Map<String, String> headers) throws Exception {
		URL u = new URL(url);
		// 创建HttpsURLConnection对象，并设置其SSLSocketFactory对象
		URLConnection.setDefaultAllowUserInteraction(true);
		URLConnection httpsConn = u.openConnection();

		Set<String> keys = headers.keySet();
		for (String k : keys) {
			httpsConn.setRequestProperty(k, headers.get(k));
		}

		// 取得该连接的输入流，以读取响应内容
		BufferedReader insr = new BufferedReader(new InputStreamReader(httpsConn.getInputStream()));

		// 读取服务器的响应内容并显示
		StringBuffer sb = new StringBuffer();
		String s;
		while ((s = insr.readLine()) != null) {
			sb.append(s + "\n");
		}
		return sb.toString();
	}

}

class ProxyAuthenticator extends Authenticator {
	private String user, password;

	public ProxyAuthenticator(String user, String password) {
		this.user = user;
		this.password = password;
	}

	protected PasswordAuthentication getPasswordAuthentication() {
		return new PasswordAuthentication(user, password.toCharArray());
	}
}
